#!/bin/sh -x

CC=""
AR=""
RANLIB=""

usage(){
  echo "
Usage:
  --os              fh8852v201-linux | linux
  --clean           whether need make clean
  --install_dir     install directory
  --help            display this help and exit
"
}

param_os="linux"
param_clean="false"
param_install_dir="`pwd`/install"

ARGS=`getopt -o "" --long os:,clean,help -- "$@"`
if [ $? != 0 ]; then
    echo "Terminating..."
    exit 1
fi

eval set -- "${ARGS}"

while true
do
    case "$1" in
        --os)
            param_os=$2;
            shift 2
            ;;
        --clean)
            param_clean="true";
            shift
            ;;
        --install_dir)
            param_install_dir=$2;
            shift 2
            ;;
        --help)
            usage;
            exit;
            ;;
        --)
            shift
            break
            ;;
        *)
            echo "Internal error!"
            exit 1
            ;;
    esac
done

if test x"${param_os}" = x"" ; then
    usage;
    exit;
fi

if [ "${param_os}" = "fh8852v201-linux" ]; then
    CC="arm-fullhanv3-linux-uclibcgnueabi-gcc"
    AR="arm-fullhanv3-linux-uclibcgnueabi-ar"
    RANLIB="arm-fullhanv3-linux-uclibcgnueabi-ranlib"
elif [ "${param_os}" = "linux" ]; then
    CC="gcc"
    AR="ar"
    RANLIB="ranlib"
else
    echo "Unsupported OS: ${param_os}"
    exit 1
fi

# param_clean == true, rm zlib-1.3.1 first
if [ "${param_clean}" = "true" ]; then
    if [ -d "zlib-1.3.1" ]; then
        rm -rf zlib-1.3.1
    fi
    if [ -d "install" ]; then
        rm -rf install
    fi
fi

# 解压
if [ ! -d "zlib-1.3.1" ]; then
    tar -xvf zlib-1.3.1.tar.xz
fi

cd zlib-1.3.1

CC=${CC} \
AR=${AR} \
RANLIB=${RANLIB} \
./configure --prefix=${param_install_dir}

make

make install
